<?php
/**
 * Description of SDimension class
 *
 * @author Chisheng Lu, Jeffy Shih
 * @copyright Copyright(c)2010 DoRISE team, Science Education center, NTNU.
 * @since 2010-03-22
 * @version 1.0
 */

class SDimension {
    private $mysqli;

    function  __construct() {
        //引入資料庫連結參數設定
        include '../inc/oaConn.inc.php';
        $this->mysqli = @new mysqli($mysqlHost,$mysqlUser,$mysqlPassword,$mysqlDB);
        if(mysqli_connect_errno()) {
            throw new Exception('無法建立資料庫連線！'.mysqli_connect_error());
            $this->mysqli = false;
            exit();
        }

        //連線使用編碼
        $this->mysqli->query("SET NAMES 'utf8'");

    }

    public function getSingleSDimension($intSDimensionId){
        //檢查向度編號是否為空白
        if (empty ($intSDimensionId)) {
            throw new Exception('缺少向度編號！');
        }

        $sql_selSDimension = 'SELECT * FROM `SDimension` WHERE `SDId`=' . $intSDimensionId;
        $rs_selSDimension = $this->mysqli->query($sql_selSDimension);

        if(!$rs_selSDimension){
            throw new Exception('無法查詢單一向度資料！');
        }else{
            if($rs_selSDimension->num_rows == 0){
                throw new Exception('無指定編號的向度資料！');
            }
            $row_selSDimension = $rs_selSDimension->fetch_row();
            return $row_selSDimension;
        }
    }

    public function getSDimensionList() {

        $arySDimensions = array();

        $sql_getSDimensionList = 'SELECT * FROM `SDimension`';
        $rs_getSDimensionList = $this->mysqli->query($sql_getSDimensionList);

        if(!$rs_getSDimensionList){
            throw new Exception('無法查詢向度資料！');
        }else{

            while($row_getSDimensionList = $rs_getSDimensionList->fetch_row()){
                array_push($arySDimensions, $row_getSDimensionList);
            }

            //釋放查詢結果
            $rs_getSDimensionList->free_result();
        }

        return $arySDimensions;
    }

    public function addSDimension($strEngName, $strChtName, $intUserId) {
        $bolRtn = false;
        //先查詢是否已有相同名稱
        if(empty ($strEngName) || empty($strChtName)){
            throw new Exception('向度名稱不可為空白！');
        }

        $sql_checkDuplicateValue = 'SELECT * FROM `SDimension` WHERE `SDEngName`=\''.$strEngName.'\' OR `SDChtName`=\''.$strChtName.'\'';
        $rs_checkDuplicateValue = $this->mysqli->query($sql_checkDuplicateValue);

        //檢查是否已有重複值
        if(!$rs_checkDuplicateValue){
            throw new Exception('查詢是否有重複時發生錯誤！');
        }else{
            if($rs_checkDuplicateValue->num_rows != 0){
                throw new Exception('已有相同的向度名稱存在！');
            }else{
                $strNow = $this->getNowTime();
                $sql_addSDimension = 'INSERT INTO `SDimension` SET `SDEngName`=\''.$strEngName.'\', `SDChtName`=\''.$strChtName.'\', `UserId`='.$intUserId . ', `SDCreated`=\''.$strNow.'\'';
                $rs_addSDimension = $this->mysqli->query($sql_addSDimension);
                if(!$rs_addSDimension){
                    throw new Exception('新增向度資料時發生錯誤！');
                }else{
                    $bolRtn = true;
                }
            }
        }

        return $bolRtn;
    }

    public function editSDimension($intSDimensionId, $strEngName, $strChtName, $intUserId) {
        $bolRtn = false;
        //檢查是否有向度編號
        if(empty ($intSDimensionId)){
            throw new Exception('缺少向度編號！');
        }

        //檢查向度名稱是否為空白
        if(empty ($strEngName) || empty ($strChtName)){
            throw new Exception('向度名稱不可為空白！');
        }

        //更新向度資料
        $strNow = $this->getNowTime();
        $sql_uptSDimension = 'UPDATE `SDimension` SET `SDEngName`=\''.$strEngName.'\', `SDChtName`=\''.$strChtName.'\', `UserId`='.$intUserId. ', `SDModified`=\''.$strNow.'\' WHERE `SDId`='.$intSDimensionId;
        if(!$this->mysqli->query($sql_uptSDimension)){
            throw new Exception('更新向度資料時發生錯誤！');
        }else{
            $bolRtn = true;
        }

        return $bolRtn;
    }

    public function delSDimension($intSDimensionId) {
        $bolRtn = false;
        //檢查向度編號是否為空白
        if (empty ($intSDimensionId)) {
            throw new Exception('缺少向度編號！');
        }
        //檢查向度是否為使用中，使用中不允許刪除
        $sql_chkUsed = 'SELECT * FROM `SQMCDIM` WHERE `DimCode`=' . $intSDimensionId;
        $rs_chkUsed = $this->mysqli->query($sql_chkUsed);
        if(!$rs_chkUsed){
            throw new Exception('異常！查詢向度是否為使用中時發生錯誤。');
        }else{
            if($rs_chkUsed->num_rows > 0){
                throw new Exception('向度項目正在使用中，不允許刪除。');
            }else{
                //非使用中的學科，先刪除該向度下層級在刪除向度本身
                $sql_delLevel = 'DELETE FROM `SDimLevel` WHERE `SDId`=' . $intSDimensionId;
                if(!$this->mysqli->query($sql_delLevel)){
                    throw new Exception('錯誤！刪除向度下的層級時發生錯誤。');
                }else{
                    //刪除學科下主題後再刪除該學科
                    $sql_delSDimension ='DELETE FROM `SDimension` WHERE `SDId`=' . $intSDimensionId;
                    if(!$this->mysqli->query($sql_delSDimension)){
                        throw new Exception('錯誤！已刪除向度下層級，於刪除向度時發生錯誤。');
                    }else{
                        $bolRtn = true;
                    }
                }
            }
        }

        return $bolRtn;
    }

    private function getNowTime() {
        return date('Y-m-d H:i:s');
    }

    public function getLevelTotalNumById($intSDimensionId){
        $intLevelCountNum = 0;

        if (empty ($intSDimensionId)) {
            throw new Exception('缺少向度編號！');
        }

        $sql_getLevelNum = 'SELECT Max(SDLOrder) FROM `SDimLevel` WHERE `SDId`=' . $intSDimensionId;
        $rs_getLevelNum = $this->mysqli->query($sql_getLevelNum);
        if(!$rs_getLevelNum){
            throw new Exception('查詢向度下層級數時發生錯誤！');
        }else{
            $row_getLevelNum = $rs_getLevelNum->fetch_row();
            $intLevelCountNum = $row_getLevelNum[0];
        }

        return $intLevelCountNum;
    }

    function  __destruct() {
        if ($this->mysqli) {
            $this->mysqli->close();
            $this->mysqli = FALSE;
        }
    }
}
?>
